package com.baoteng.base.component;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.baoteng.base.R;
import com.baoteng.base.model.StateResource;
import com.baoteng.ui.progress.CustomProgressDialog;
import com.baoteng.ui.progress.ProgressSupport;
import com.baoteng.ui.toast.BaseToast;

import java.lang.ref.WeakReference;

public class BaseFragment extends Fragment implements ProgressSupport {
    protected Context mContext;
    private final Handler uiHandler = new Handler(Looper.getMainLooper());
    protected FragmentActivity mActivity;
    private WeakReference<Context> mAppContextRef;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
        if (mContext != null) {
            mAppContextRef = new WeakReference<>(mContext.getApplicationContext());
        }
    }


    @Override
    public void onDetach() {
        super.onDetach();
        if (mActivity instanceof BaseActivity) {
            ((BaseActivity) mActivity).dismissProgress();
        }
        mActivity = null;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (FragmentActivity) context;
    }

    @Nullable
    @Override
    public Context getContext() {
        Context context = super.getContext();
        if (context == null && mAppContextRef != null) {
            context = mAppContextRef.get();
        }
        return context;
    }

    @Override
    public CustomProgressDialog getProgressDialog() {
        if (mActivity instanceof BaseActivity) {
            return ((BaseActivity) mActivity).getProgressDialog();
        }
        return null;
    }

    protected void showProgressDialog(@StringRes int resId) {
        if (isDestroy()) {
            return;
        }
        String content = getString(resId);
        showProgressDialog(content);
    }

    @Override
    public boolean isDestroy() {
        final Activity activity = getActivity();
        if (activity != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                return activity.isFinishing() || activity.isDestroyed();
            }
        }
        final Context context = getContext();
        if (context != null) {
            return !isAdded();
        }
        return true;
    }

    @Override
    public Handler getUIHandler() {
        return uiHandler;
    }

    protected void showProgressDialog(final String content) {
        if (isDestroy()) {
            return;
        }
        if (mActivity instanceof BaseActivity) {
            ((BaseActivity) mActivity).showProgressDialog(content);
        }
    }

    protected void dismissProgress() {
        if (isDestroy()) {
            return;
        }
        if (mActivity instanceof BaseActivity) {
            ((BaseActivity) mActivity).dismissProgress();
        }
    }

    protected void showToast(@StringRes int strRes) {
        try {
            showToast(getString(strRes));
        } catch (Exception ignore) {
        }
    }

    protected void showToast(String content) {
        if (isDestroy()) {
            return;
        }
        BaseToast.showToast(getActivity(), content);
    }

    protected void showToastException(@StringRes int strRes) {
        try {
            showToastException(getString(strRes));
        } catch (Exception ignore) {
        }
    }

    protected void showToastException(String content) {
        if (isDestroy()) {
            return;
        }
        BaseToast.toastException(getActivity(), content);
    }

    protected View onCreateWrapView(View childView) {
        View rootView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_base_state, null);
        FrameLayout fullContentLayout = rootView.findViewById(R.id.full_content_layout);
        fullContentLayout.addView(childView);
        return rootView;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    public class SimpleStateCallback<T> implements StateResource.OnHandleCallback<T> {

        @Override
        public void onLoading() {
            showProgressDialog("");
        }

        @Override
        public void onSuccess(T data) {

        }

        @Override
        public void onFailure(String message) {

        }

        @Override
        public void onCompleted() {
            dismissProgress();
        }
    }
}
